﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Telerik.Web.UI;
using TravelAgency.DAL;

namespace TravelAgency.AspNet.Admins
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Session["userId"] = Membership.GetUser().ProviderUserKey;
        }

        protected string UserNameWordWrapEval(string PropertyName)
        {
            string fullUserName = Eval(PropertyName).ToString();
            if (fullUserName.Length > 50)
            {
                string shortTitle = fullUserName.Substring(0, 50);
                return shortTitle + "...";
            }
            else
            {
                return fullUserName;
            }
        }

        protected string GetFirstRoleOfUser(string propertyName)
        {
            string username = (string)Eval(propertyName);

            if (Roles.GetRolesForUser(username).Length != 0)
            {
                string firstRole = Roles.GetRolesForUser(username)[0];
                return firstRole;
            }
            else
            {
                return "Customer";
            }
        }

        protected void LinkButtonYas_Click(object sender, EventArgs e)
        {
            Guid userId = (Guid)this.RadListViewUsers.SelectedItems[0].GetDataKeyValue("UserId");
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                dao.DeleteReservationsByUserId(userId);
                dao.SaveChanges();
            }

            string username = (string)this.RadListViewUsers.SelectedItems[0].GetDataKeyValue("UserName");
            Membership.DeleteUser(username);

            this.Response.Redirect("~/Admins/ManageUsers.aspx");
        }

        protected void RadListViewUsers_ItemCreated(object sender, RadListViewItemEventArgs e)
        {
            if (e.Item is RadListViewEmptyDataItem)
            {
                return;
            }

            RadListViewDataItem dataItem = (RadListViewDataItem)e.Item;
            string username = dataItem.GetDataKeyValue("UserName").ToString();
            if (e.Item is RadListViewEditableItem && e.Item.IsInEditMode)
            {
                if (Roles.GetRolesForUser(username).Length > 0)
                {
                    string userFirstRole = Roles.GetRolesForUser(username)[0];

                    if (userFirstRole == "Admin")
                    {
                        ((LinkButton)e.Item.FindControl("LinkButtonGrandRole")).Visible = false;
                        ((LinkButton)e.Item.FindControl("LinkButtonSuspendRole")).Visible = false;
                        ((Label)e.Item.FindControl("LabelNoEditAction")).Visible = true;
                        ((LinkButton)e.Item.FindControl("LinkButtonCancelBack")).Text = "Back";
                    }
                    else
                    {
                        if (userFirstRole == "Agent")
                        {
                            ((LinkButton)e.Item.FindControl("LinkButtonGrandRole")).Visible = false;
                            ((LinkButton)e.Item.FindControl("LinkButtonSuspendRole")).Visible = true;
                            ((Label)e.Item.FindControl("LabelNoEditAction")).Visible = false;
                            ((LinkButton)e.Item.FindControl("LinkButtonCancelBack")).Text = "Cancel";
                        }
                        else
                        {
                            ((LinkButton)e.Item.FindControl("LinkButtonGrandRole")).Visible = true;
                            ((LinkButton)e.Item.FindControl("LinkButtonSuspendRole")).Visible = false;
                            ((Label)e.Item.FindControl("LabelNoEditAction")).Visible = false;
                            ((LinkButton)e.Item.FindControl("LinkButtonCancelBack")).Text = "Cancel";
                        }
                    }
                }
                else
                {
                    ((LinkButton)e.Item.FindControl("LinkButtonGrandRole")).Visible = true;
                    ((LinkButton)e.Item.FindControl("LinkButtonSuspendRole")).Visible = false;
                    ((Label)e.Item.FindControl("LabelNoEditAction")).Visible = false;
                    ((LinkButton)e.Item.FindControl("LinkButtonCancelBack")).Text = "Cancel";
                }
            }
        }

        protected void GrandRoleAgent_Click(object sender, EventArgs e)
        {
            if (this.Session["usename"] != null)
            {
                try
                {
                    string username = this.Session["usename"].ToString();
                    if (Roles.IsUserInRole(username, "Customer"))
                    {
                        Roles.RemoveUserFromRole(username, "Customer");
                    }
                    if (Roles.IsUserInRole(username, "Admin"))
                    {
                        Roles.RemoveUserFromRole(username, "Admin");
                    }

                    if (Roles.IsUserInRole(username, "Agent"))
                    {
                        Roles.RemoveUserFromRole(username, "Agent");
                    }

                    Roles.AddUserToRole(username, "Agent");
                    this.Response.Redirect("~/Admins/ManageUsers.aspx");
                }
                catch (Exception ex)
                {                    
                    throw ex;
                }
            }
        }

        protected void SuspendRoleAgentGrandRoleCustomer_Click(object sender, EventArgs e)
        {
            if (this.Session["usename"] != null)
            {
                try
                {
                    string username = this.Session["usename"].ToString();

                    if (Roles.IsUserInRole(username, "Agent"))
                    {
                        Roles.RemoveUserFromRole(username, "Agent");
                    }
                    if (Roles.IsUserInRole(username, "Admin"))
                    {
                        Roles.RemoveUserFromRole(username, "Admin");
                    }
                    if (Roles.IsUserInRole(username, "Customer"))
                    {
                        Roles.RemoveUserFromRole(username, "Customer");
                    }

                    Roles.AddUserToRole(username, "Customer");
                    this.Response.Redirect("~/Admins/ManageUsers.aspx");
                }
                catch(Exception ex)
                {
                    throw ex;
                }

            }
        }

        protected void RadListViewUsers_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.ListViewItem is RadListViewDataItem)
            {
                RadListViewDataItem dataItem = (RadListViewDataItem)e.ListViewItem;
                string username = dataItem.GetDataKeyValue("UserName").ToString();
                this.Session["usename"] = username;
            }
        }

        protected void LinkButtonSearch_Click(object sender, EventArgs e)
        {
            string searchPhrase = this.TextBoxSearch.Text;
            this.RadListViewUsers.FilterExpressions.Clear();
            this.RadListViewUsers.FilterExpressions.BuildExpression().
                Contains("UserName", searchPhrase).Build();
            this.RadListViewUsers.Rebind();
        }        
    }
}